<template>
    <el-card shadow="always" :body-style="{ padding: '20px', height: '700px' }" v-loading="loading">
        <template #header>
            <span>访问页面统计</span>
            <el-button type="primary" size="small" @click="accessData" class="title-btn">加载数据</el-button>
        </template>
        <ECharts ref="chart" :option="option"/>
    </el-card>
</template>

<script>
    import { h } from 'vue';
    import * as echarts from 'echarts';
    import { createComponent } from 'echarts-for-vue';
    import axios from 'axios'

    export default {
        name: 'AccessPageUsage',
        components: {
            ECharts: createComponent({ echarts, h }), 
        },
        data() {
            return {
                loading: false,
                dateReady: false,
                option: {
                    title: {
                        text: '页面访问统计',
                        subtext: '数据未加载',
                        left: 'center'
                    },
                    xAxis: {
                        type: 'category',
                        data: ['资讯', '文章', '需求', '供应', '其他']
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [{
                        data: [0, 0, 0, 0, 0],
                        type: 'bar',
                        showBackground: true,
                        backgroundStyle: {
                            color: 'rgba(220, 220, 220, 0.8)'
                        }
                    }]
                },
            };
        },
        methods: {
            async accessData(){
                this.loading = true;
                let res = await axios.get('http://localhost/api/usage/getAccessPageUsage')
                .then(res => res.data)
                .catch(err => {
                    console.error(err); 
                })
                this.loading = false;
                this.option.series[0].data = res.data
                this.option.title.subtext = res.yestoday
                this.charts.setOption(this.option, true, false)
            }
        },
        mounted() {
            this.charts = this.$refs.chart
        },
    }
</script>


<style lang="scss" scoped>
.title-btn {
    float: right;
    margin-right: 20px;
}
</style>